package com.example.wechatappbackend.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.wechatappbackend.VO.DishVO;
import com.example.wechatappbackend.VO.MeuaVO;
import com.example.wechatappbackend.VO.OrderVO;
import com.example.wechatappbackend.entity.Dish;
import com.example.wechatappbackend.entity.DishSpecies;
import com.example.wechatappbackend.entity.Order;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.List;

public  class Utils {
    public static OrderVO orderToOrderVO(Order order){
        OrderVO orderVO = new OrderVO();

        orderVO.setOrderId(order.getOrderId());
        orderVO.setStatus(order.getStatus());
        orderVO.setRestaurantName(order.getRestaurantName());
        orderVO.setRestaurantAvatar(order.getRestaurantAvatar());
        orderVO.setOrderPrice(order.getOrderPrice());
        orderVO.setOrderTime(order.getOrderTime());
        return orderVO;
    }

    public static DishVO dishToDishVO(Dish dish, DishSpecies dishSpecies){
        DishVO dishVO = new DishVO();

        dishVO.setID(dish.getDishID());
        dishVO.setTitle(dish.getDishName());
//        dishVO.setCategoryID(dish.getCategoryID());
        dishVO.setImgUrl(dish.getPicture());
        dishVO.setPrice(dish.getPrice());
//        dishVO.setQuantity(dish.getQuantity());
        dishVO.setDescription(dish.getDescription());
        dishVO.setChooseNumber(0);
//        dishVO.setOption1(dish.getOption1());
//        dishVO.setOption2(dish.getOption2());
//        dishVO.setOption3(dish.getOption3());
//        dishVO.setOption4(dish.getOption4());
//        dishVO.setStatus(dish.getStatus());
//        dishVO.setRecommend(dish.getRecommend());
//        dishVO.setSpecies(dishSpecies.getSpecies());
//        dishVO.setSpeciesDesc(dishSpecies.getSpeciesDesc());

        return dishVO;
    }

    public static MeuaVO createMeua(String name, List<DishVO> dishVO){
        MeuaVO meuaVO = new MeuaVO();
        meuaVO.name = name;
        meuaVO.data = dishVO;
        return meuaVO;
    }

    public static String tuchuang() throws Exception {

        String url = "https://api.uomg.com/api/image.ali?imgurl="+"D:\\data\\code\\shixun\\gitee\\makeorder-wx-master\\makeorder-wx-master\\static\\img\\category\\01d8df2fc7ae0a5d9e5b8d4e6946eedf.png";
        URLConnection urlConnection = new URL(url).openConnection();
        HttpURLConnection connection = (HttpURLConnection) urlConnection;
        connection.setRequestMethod("GET");
        connection.connect();

        int responseCode = connection.getResponseCode();
        JSONObject jsonObject = null;
        if (responseCode == HttpURLConnection.HTTP_OK) {
//            JSONObject jsonObject = new JSONObject(new  InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader
                    (connection.getInputStream(), StandardCharsets.UTF_8));
            StringBuilder bs = new StringBuilder();
            String l;
            while ((l = bufferedReader.readLine()) != null) {
                bs.append(l).append("\n");
            }
            String content = null;
            content = bs.toString();
            System.out.println("content:"+content);
            jsonObject =  JSON.parseObject(content);
        }
        return null;
    }

}
